package com.xyht.sca_s.student_manage_system.modules.trainingSchool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.trainingSchool.entity.SmsTsProfession;
import com.xyht.sca_s.student_manage_system.modules.trainingSchool.entity.req.SmsTsProfessionReq;
import com.xyht.sca_s.student_manage_system.modules.trainingSchool.entity.resp.SmsTsProfessionResp;
import com.xyht.sca_s.student_manage_system.modules.trainingSchool.mapper.SmsTsProfessionMapper;
import com.xyht.sca_s.student_manage_system.modules.trainingSchool.service.SmsTsProfessionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 行业表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-05-15
 */
@Service
public class SmsTsProfessionServiceImpl extends ServiceImpl<SmsTsProfessionMapper, SmsTsProfession> implements SmsTsProfessionService {

    @Override
    public ResponseResult add(SmsTsProfessionReq req) {
        if (isNullOrEmpty(req.getCode(), req.getName())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        // 查询数据库中是否有重复值
        int count = this.count(new LambdaQueryWrapper<SmsTsProfession>()
//                .eq(SmsTsProfession::getCode, req.getCode())
                .eq(SmsTsProfession::getName, req.getName()));
        if (count > 0) {
            return CommonResult.failed(CommonCodeEnum.DATA_EXIST);
        }

        SmsTsProfession profession = new SmsTsProfession();
        BeanUtils.copyProperties(req, profession, "id");
        this.save(profession);

        return CommonResult.success(profession.getId());
    }

    @Override
    public ResponseResult list(Integer pageNum, Integer pageSize, String ss) {
        LambdaQueryWrapper<SmsTsProfession> queryWrapper = new LambdaQueryWrapper<SmsTsProfession>()
                .orderByAsc(SmsTsProfession::getCode);

        if (!isNullOrEmpty(ss)) {
            queryWrapper.like(SmsTsProfession::getName, ss);
        }

        Page<SmsTsProfession> page = new Page<>(pageNum, pageSize);
        this.page(page, queryWrapper);
        List<SmsTsProfessionResp> collect = page.getRecords().stream().map(item -> {
            SmsTsProfessionResp resp = new SmsTsProfessionResp();
            BeanUtils.copyProperties(item, resp);
            return resp;
        }).collect(Collectors.toList());

        return CommonResult.success(collect, (int) page.getTotal());
    }

    @Override
    public ResponseResult update(SmsTsProfessionReq req) {
        if (isNullOrEmpty(req.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsTsProfession profession = this.getById(req.getId());
        if (profession == null) {
            return CommonResult.failed(CommonCodeEnum.SCHOOL_TRAINING_PROFESSION_NOT_EXIST);
        }

        BeanUtils.copyProperties(req, profession);
        this.save(profession);

        return CommonResult.success();
    }
}
